﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace QMSDAL
{
    public class Users
    {
        //protected CurUser curUser;
        public static string cnstr = ConfigurationManager.ConnectionStrings["HHMSYS"].ConnectionString.ToString();
        /// <summary>
        /// 获取所有人员
        /// </summary>
        /// <returns></returns>
        public static DataSet GetAllUsers()
        {
            using (SqlConnection cn = new SqlConnection(cnstr))
            {
                DataSet ds = new DataSet();
                cn.Open();
                string strSQL = "SELECT FPERSON, FDISPLAYNAME, DEPARTMENT FROM TORGSYSTEM";
                SqlDataAdapter cmd = new SqlDataAdapter(strSQL, cn);
                cmd.Fill(ds);

                return ds;
            }
        }

        /// <summary>
        /// 获取满足条件的一个用户
        /// </summary>
        /// <param name="employeeId"></param>
        /// <returns></returns>
        public static DataSet GetAUser(string employeeId)
        {
            using (SqlConnection cn = new SqlConnection(cnstr))
            {
                DataSet ds = new DataSet();
                cn.Open();
                string strSQL = "SELECT FPERSON, FDISPLAYNAME, DEPARTMENT FROM TORGSYSTEM WHERE FPERSON = '" 
                    + employeeId + "'";
                SqlDataAdapter cmd = new SqlDataAdapter(strSQL, cn);
                cmd.Fill(ds);

                return ds;
            }
        }

        /// <summary>
        /// 根据当前用户的id获取用户姓名和部门信息
        /// </summary>
        /// <param name="employeeId"></param>
        /// <returns></returns>
        public static CurUser GetCurUser(string employeeId)
        {
            CurUser curUser = new CurUser();
            
            using (SqlConnection cn = new SqlConnection(cnstr))
            {
                cn.Open();
                string strSQL = "SELECT FPERSON, FDISPLAYNAME, DEPARTMENT FROM TORGSYSTEM WHERE FPERSON = '"
                    + employeeId + "'";
                SqlCommand cmd = new SqlCommand(strSQL, cn);
                SqlDataReader myReader = cmd.ExecuteReader();
                while (myReader.Read())
                {
                    curUser.employeeId = myReader["FPERSON"].ToString();
                    curUser.name = myReader["FDISPLAYNAME"].ToString();
                    curUser.department = myReader["DEPARTMENT"].ToString();
                }
            }
            return curUser;
        }

        /// <summary>
        /// 根据当前用户的id和密码获取用户姓名和部门信息
        /// </summary>
        /// <param name="employeeId"></param>
        /// <returns></returns>
        public static CurUser GetCurUser(string employeeId, string password)
        {
            CurUser curUser = new CurUser();
            using (SqlConnection cn = new SqlConnection(cnstr))
            {
                cn.Open();
                string strSQL = "SELECT FPERSON, FDISPLAYNAME, DEPARTMENT FROM TORGSYSTEM WHERE FPERSON = '"
                    + employeeId + "' and PASSWORD = '" + password + "'";
                SqlCommand cmd = new SqlCommand(strSQL, cn);
                string str = cmd.ExecuteScalar().ToString();
                if (str != null)
                {
                    SqlDataReader myReader = cmd.ExecuteReader();
                    while (myReader.Read())
                    {
                        curUser.employeeId = myReader["FPERSON"].ToString();
                        curUser.name = myReader["FDISPLAYNAME"].ToString();
                        curUser.department = myReader["DEPARTMENT"].ToString();
                    }
                }
            }
            return curUser;
        }
    }

    public struct CurUser
    {
        public string employeeId;
        public string name;
        public string department;

        //public string employeeId { get; set; }
        //public string name { get; set; }
        //public string department { get; set; }
    };
}
